layui.use(['element', 'tree', 'zlContext', 'zlConfig', 'zlPopSourceForm'], function (exports) {
    var $ = layui.$,
        zlContext = layui.zlContext,
        layer = layui.layer,
        zlConfig = layui.zlConfig,
        zlPopSourceForm = layui.zlPopSourceForm,
        statusName = zlConfig.global.result.statusName,
        msgName = zlConfig.global.result.msgName,
        OK = zlConfig.global.result.okState,
        form = layui.form;

    var frm = [
        '<form class="layui-form " style="margin:8px" id="addnew" >'
        , '   <input type="hidden" name="ORGID" value="9C876276D8884BFF94DF343E2A4EED7D">'
        , '   <input type="hidden" name="RWID">'
        , '   <input type="hidden" name="RES_TYPE" value="DED487754B9241B5AA08124385D069C7">'
        , '   <input type="hidden" name="type">'
        , '   <div class="layui-form-item">'
        , '       <div class="layui-col-md12 ">'
        , '           <label class="layui-form-label">名称</label>'
        , '           <div class="layui-input-block">'
        , '                 <input type="text" class="layui-input" lay-filter="OBJNAME" name="OBJNAME" maxlength="40" lay-verify="required">'
        , '           </div>'
        , '       </div>'
        , '   </div>'
        , '   <div class="layui-form-item">'
        , '       <div class="layui-col-md12">'
        , '           <label class="layui-form-label">图标</label>'
        , '           <div class="layui-input-block" id="smalldiv">'
        , '               <div name="ICON_SMALL" zlcomponent lay-verify="required" class="layui-form-select zlPopSourceForm">'
        , '               </div>'
        , '           </div>'
        , '       </div>'
        , '   </div>'
        , '   <div class="layui-form-item">'
        , '       <div class="layui-col-md12">'
        , '           <label class="layui-form-label">功能链接</label>'
        , '           <div class="layui-input-block">'
        , '               <input  type="text" name="RES_ACTION"  class="layui-input" maxlength="80" lay-verify="required">'
        , '           </div>'
        , '       </div>'
        , '   </div>'
        , '   <div class="layui-form-item">'
        , '       <div class="layui-input-block">'
        , '           <button class="layui-btn"  id="btnadd" lay-submit  lay-filter="addfunction">保存</button>'
        , '       </div>'
        , '   </div>'
        , '</form>'
    ],
        formindex,
        $curView,
        newFormObj = {};

    var iconsetting = "{shiycjid:'8C2506793EAE4A5CAB268E07E68BF3B4',isOpenAdQ:true,edittype:'readonly',type:'image'," +
        'coltitle:[[' +
        '{ type: "numbers", fixed: "left", title: "#" },' +
        '{ type: "radio", fixed: "left" },' +
        '{ field: "WJZY_MINGC", title: "名称", width: 100 },' +
        '{ field: "WJZY_BIANH", title: "编号", width: 150 },' +
        '{ field: "WJZY_ZIYLJ", title:"资源", width: 150,templet: "<div><img style=' + "'" + 'width:auto;height:auto;max-width:100%;max-height:100%' + "'" + ' atl=' + "''" + "src='" + layui.cache['contentPath'] + "{{ d.WJZY_ZIYLJ}}" + "'" + '></div>"}]],' +
        "limit:10,formtitle:'选择项目图片', placeholder:'请选择项目图片',page:true, id:'RWID',name : 'WJZY_ZIYLJ', method:'post',params:'{WJZY_LEIX:1}' ,dataurl:'" + layui.cache['contentPath'] + "/manager/corefilesres/query'}";
    // 展开或折叠全部

    function setclickevent() {
        //避免重复
        $('.new').unbind('click');
        $('.close').unbind('click');
        $('.title').unbind('click');

        $('.new').on('click', function () {
            $curView = $(this);
            var showform = frm.join(''),
                that = $(this);

            formindex = layer.open({
                type: 1,
                content: showform,
                title: $(this).attr("rwid") ? "编辑系统" : "新增系统",
                id: 'addSystemLayer',
                area: ['100%', '100%'],
                success: function (index) {
                    iconsetting = iconsetting.replace(/readonly/gi, 'edit');
                    if (that.attr("rwid")) {
                        $('input[name="type"]').val('edit');
                        $('input[name="OBJNAME"]').val(that.next().text());
                        $('div[name="ICON_SMALL"]').attr("value", that.attr("icon"));
                        $('input[name="RES_ACTION"]').val(that.attr("resaction"));
                        $('input[name="RWID"]').val(that.attr("rwid"));
                        $('div[name="ICON_SMALL"]').attr('text', that.attr("src"));
                    } else {
                        $('input[name="type"]').val('add');
                    }

                    $('div[name="ICON_SMALL"]').attr('settings', iconsetting);
                    form.render();
                    if (zlContext.browserType() == 'IE') {
                        zlPopSourceForm.render($('div[name=ICON_SMALL]'));
                    }
                }
            });
        });

        $('.close').on('click', function () {
            var rwid = $(this).parent('.context').find('img').attr('rwid'),
                that = $(this);
            zlContext.confirm('询问', '是否要删除该系统？', function (index) {               
                if (rwid) {
                    zlContext.deletePost(layui.cache['contentPath'] + '/manager/coreproject/remove', { RWID: rwid }, function (data) {
                        if (data[statusName] == OK) {
                            zlContext.successMsg('删除成功！');
                            that.parent('.context').parent('.layui-col-md2').remove();
                        } else {
                            zlContext.errorMsg(data[msgName]);
                        }
                    })
                }
                layer.close(index);
            });
        });
        $('.title').on('click', function () {
            $('.new').trigger('click');
        });
    }

    function getdata() {
        zlContext.post(layui.cache['contentPath'] + "/manager/coreproject/query", {}, function (data) {
            if (data[statusName] == OK) {
                var datalist = data['obj'];
                for (var i = 0; i < datalist.length; i++) {
                    var curdata = datalist[i];
                    var html = [
                        '<div class="layui-col-md2">'
                        , '    <div class="context">'
                        , '        <img class="new" icon="' + curdata.ICON_SMALL + '" resaction="' + curdata.RES_ACTION + '" rwid="' + curdata.RWID + '"src="' + layui.cache['contentPath'] + curdata.ICON_SMALL_URL + '" alt="添加">'
                        , '        <p class="title">' + curdata.OBJNAME + '</p>'
                        , '        <i class="layui-icon layui-icon-close close"></i>'
                        , '    </div>'
                        , '</div>'
                    ]
                    $('.layui-row').append(html.join(''));
                }
                var addhtml = [
                    '<div class="layui-col-md2">'
                    , '    <div class="context">'
                    , '        <img class="new" src="' + layui.cache['contentPath'] + '/static/image/add.png" alt="添加">'
                    , '        <p class="title"></p>'
                    , '    </div>'
                    , '</div>'
                ]
                $('.layui-row').append(addhtml.join(''));
                setclickevent();
            }
            else {
                zlContext.errorMsg(data[msgName]);
            }
        })
    }
    getdata();

    form.on('submit(addfunction)', function (data) {
        if ($(data.elem).hasClass('getField')) {
            newFormObj.newFields = data.field
            return false;
        }
        var type = $('input[name="type"]').val();
        var url = layui.cache['contentPath'] + "/manager/coreproject/add";
        if (type == "edit") {
            url = layui.cache['contentPath'] + "/manager/coreproject/modify";
        }
        zlContext.post(
            url,
            data.field,
            function (newdata) {
                if (newdata[statusName] == OK) {
                    newdata.obj.img = $('img[name="inputICON_SMALL"]').attr("src");
                    layer.close(formindex);
                    zlContext.successMsg('保存成功');
                    buildview(newdata.obj, type)
                    return false;
                } else {
                    zlContext.errorMsg(newdata[msgName]);
                }
            });
        return false;
    });

    function buildview(data, type) {
        $curView.attr("src", data.img);
        $curView.next().text(data.OBJNAME);
        $curView.attr("rwid", data.RWID);
        $curView.attr("resaction", data.RES_ACTION);
        $curView.attr("icon", data.ICON_SMALL);
        if (type == 'add') {
            var html = [
                '<div class="layui-col-md2">'
                , '    <div class="context">'
                , '        <img class="new" src="' + layui.cache['contentPath'] + '/static/image/add.png" alt="添加">'
                , '        <p class="title"></p>'
                , '    </div>'
                , '</div>'
            ]
            $('.layui-row').append(html.join(''));
            $curView.parent('.context').append('<i class="layui-icon layui-icon-close close"></i>');
            setclickevent();
        }
    }

    $(window).on('resize', function () {
        zlContext.setMaxHeihgt(0);
    }).resize();


});
